package chapter11;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-22 下午5:00
 */
public class CopyDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(CopyDemo.class);

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String fileSrc = File.separator + "home" + File.separator + "lzy" + File.separator + "logs" + File.separator + "test" + File.separator + "test.dat";
        String fileDest = File.separator + "home" + File.separator + "lzy" + File.separator + "logs" + File.separator + "test" + File.separator + "test_copy.dat";

        File file = new File(fileSrc);
        if (!file.exists()) {
            LOGGER.info("source file is not exit!");
            System.exit(1);
        }

        File dest = new File(fileDest);
        if (dest.exists()) {
            dest.delete();
        }

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = new FileOutputStream(dest)) {
            int temp = 0;
            byte[] data = new byte[20];
            while ((temp = inputStream.read(data)) != -1) {
                outputStream.write(data, 0, temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        LOGGER.info("consume time:{} ms", start - end);
    }
}
